# Python项目实战之迭代器实现字符串的逆序输出
'''
class Reverse:
    def __init__(self, string):
        self.__string = string
        self.__index = len(string)
    
    def __iter__(self):
        return self
    
    def __next__(self):
        self.__index -= 1 # 没有对循环结束进行判断，会出现：IndexError: string index out of range
        return self.__string[self.__index]

revstr = Reverse("Python")
for c in revstr:
    print(c, end = " ")
'''

class Reverse:
    def __init__(self, string):
        self.__string = string
        self.__index = len(string)
    
    def __iter__(self):
        return self
    
    def __next__(self):
        if self.__index == 0:
            raise(StopIteration)
        self.__index -= 1
        return self.__string[self.__index]

revstr = Reverse("Python")
for c in revstr:
    print(c, end = " ")
